<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>下面五个点控制渐变式轮播图</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            list-style-type: none;
        }
        
        .lunbo {
            width: 1226px;
            height: 460px;
            margin: 0 auto;
            position: relative;
        }
        
        .lunbo>.imgLists {
            width: 100%;
            height: 100%;
        }
        
        .lunbo>.imgLists>.imgItem {
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            transition: all 1s;
        }
        
        .lunbo>.imgLists>.imgItem.active {
            opacity: 1;
        }
        
        .lunbo>.imgLists>.imgItem>img {
            width: 100%;
            height: 100%;
        }
        
        .lunbo>.preBtn {
            width: 40px;
            height: 40px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            top: calc(50% - 20px);
            left: 0;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
        }
        
        .lunbo>.nextBtn {
            width: 40px;
            height: 40px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            top: calc(50% - 20px);
            right: 0;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
        }
        
        .lunbo>.pots {
            width: 300px;
            height: 40px;
            position: absolute;
            left: calc(80% - 150px);
            bottom: 10%;
        }
        
        .lunbo>.pots>.pot {
            width: 15px;
            height: 15px;
            margin: 0 15px;
            float: left;
            border-radius: 10px;
            font-size: 15px;
            line-height: 15px;
            text-align: center;
            background-color: #fff;
        }
    </style>
</head>

<body>
    <div class="lunbo">
        <div class="imgLists">
            <div class="imgItem active"><img src="../img/mi1.jpg" alt=""></div>
            <div class="imgItem"><img src="../img/mi2.webp" alt=""></div>
            <div class="imgItem"><img src="../img/mi3.webp" alt=""></div>
            <div class="imgItem"><img src="../img/mi4.webp" alt=""></div>
            <div class="imgItem"><img src="../img/mi5.webp" alt=""></div>
        </div>
        <div class="preBtn">
            <</div>
                <div class="nextBtn">></div>
                <!--轮播图下面五个点-->
                <ul class="pots">
                    <li class="pot" style="background-color:red" id="circle0">1</li>
                    <li class="pot" id="circle1">2</li>
                    <li class="pot" id="circle2">3</li>
                    <li class="pot" id="circle3">4</li>
                    <li class="pot" id="circle4">5</li>
                </ul>
        </div>
        <script>
            var lunbo = document.querySelector(".lunbo");
            var preBtn = document.querySelector(".preBtn");
            var nextBtn = document.querySelector(".nextBtn");
            var imgItem = document.querySelectorAll(".imgItem");
            var pot = document.querySelectorAll(".pot");
            //一。左点击时间
            var currentImg = 0; //记录当前照片第几张
            preBtn.onclick = function(e) {
                    //console.log(e);
                    currentImg--;
                    if (currentImg < 0) {
                        currentImg = imgItem.length - 1;
                    }
                    renderImg();
                }
                //二。右点击事件
            nextBtn.onclick = function(e) {
                //console.log(e);
                currentImg++;
                if (currentImg > imgItem.length - 1) {
                    currentImg = 0;
                }
                renderImg();
            }

            //封装渲染时间
            
            function renderImg() {
                //1.先将所有图片的active都出掉
                imgItem.forEach(function(item, index) {
                    //console.log(item + index);
                    item.classList.remove("active");

                })
                pot.forEach(function(item, index) {
                    //console.log(item);
                    item.style.backgroundColor = "#fff";
                });
                //2.讲下一个图片添加上active
                imgItem[currentImg].classList.add("active");
                pot[currentImg].style.backgroundColor = "red";
            }

            //小圆点点击事件
            //解决办法一：利用在id中埋点获取当前的index
            /* pot.forEach(function (item, index) {
                 item.onclick = function () {
                     var index = parseInt(item.id[6]);//采取巧妙的办法获得index，直接取id方式获得index
                     currentImg = index;
                     console.log(index);
                     renderImg();
                 }
             })*/
            //解决办法二：forEach(function（item,index）{})本身自带的index
            /* pot.forEach(function (item, index) {
                 item.onclick = function () {
                     var index = parseInt(item.id[6]);//采取巧妙的办法获得index，直接取id方式获得index
                     currentImg = index;
                     console.log(index);
                     renderImg();
                 }
             })*/
            //解决办法三：for循环，利用在id中埋点获取当前的index
            /*
             * 因为for循环中的i循环结束就是5，所以点击任何地方i都是5
             * */
            /*for (var i = 0; i < pot.length; i++) {
                pot[i].onclick = function () {
                   //var i = parseInt(this.id[6]);//采取巧妙的办法获得index，直接取id方式获得index
                    currentImg = i;
                    console.log(i);
                    renderImg();
                }
            }*/
            //解决办法四：立即执行函数
            for (var i = 0; i < pot.length; i++) {
                pot[i].onclick = (function(index) {
                    //使用立即执行函数将i传给参数index,因为最后要返回的是个函数，所以要return一个函数
                    return function() {
                        currentImg = index;
                        console.log(index);
                        renderImg();
                    }
                })(i);
            }
        </script>
</body>

</html>